package com.nzf.vote.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nzf.vote.entity.User;
import com.nzf.vote.entity.UserVote;
import com.nzf.vote.entity.Vote;
import com.nzf.vote.mapper.UserVoteMapper;
import com.nzf.vote.model.dto.OptionDto;
import com.nzf.vote.model.dto.VoteDto;
import com.nzf.vote.model.dto.VoteListDto;
import com.nzf.vote.model.vo.Results;
import com.nzf.vote.service.ITypeService;
import com.nzf.vote.service.IUserService;
import com.nzf.vote.service.IVoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author nzf
 * @since 2022-07-25
 */
@CrossOrigin
@RestController
@RequestMapping("/vote")
public class VoteController {
    @Autowired
    IVoteService service;
    @Autowired
    ITypeService typeService;
    @Autowired
    IUserService userService;
    @Autowired
    UserVoteMapper userVoteMapper;

    @GetMapping("/getVoteList/{pageIndex}/{size}")
    public Results getVoteList(@PathVariable Integer pageIndex,@PathVariable Integer size){
        Page<Vote> votePage = new Page<>(pageIndex,size);
        Page<Vote> page = new LambdaQueryChainWrapper<>(service.getBaseMapper()).page(votePage);
        List<VoteDto> voteDtos =new ArrayList<>();
        page.getRecords().forEach(vote -> {
            VoteDto voteDto = new VoteDto();
            voteDto.setId(vote.getId());
            voteDto.setTheme(vote.getTheme());
            voteDto.setNum(vote.getNum());
            voteDto.setThemeInfo(vote.getThemeInfo());
            voteDto.setThemeType(typeService.getById(vote.getThemeTypeId()).getType());
            voteDto.setUsername(userService.getById(vote.getUserId()).getName());
            String[] split = vote.getMyOption().split(",");
            List<OptionDto> optionDto = new ArrayList<>();
            List<UserVote> userVoteByVoteId = userVoteMapper.getUserVoteByVoteId(vote.getId());
            voteDto.setCreateTime(vote.getCreateTime());
            for (int i = 0; i < split.length; i++) {
                int finalI = i;
                int num = (int) userVoteByVoteId.stream().filter(userVote -> userVote.getMyOption().equals(split[finalI])).count();
                OptionDto p = new OptionDto();
                p.setOption(split[i]);
                p.setNum(num);
                optionDto.add(p);
            }
            voteDto.setOption(optionDto);
            voteDtos.add(voteDto);
        });
        VoteListDto voteListDto = new VoteListDto();
        voteListDto.setVoteDto(voteDtos);
        voteListDto.setTotal(page.getTotal());
        return Results.success(voteListDto);
    }

    @GetMapping("getVoteList")
    public Results getVoteLists(){
        return Results.success(service.VoteList());
    }
    @GetMapping("getVoteByUserId/{userId}")
    public Results getVoteByUserId(@PathVariable Integer userId){
        return Results.success(service.getVoteByUserId(userId));
    }
    @GetMapping("getVoteByUserId/{userId}/{pageIndex}/{size}")
    public Results getVoteListByUserId(@PathVariable Integer userId,@PathVariable Integer pageIndex,@PathVariable Integer size){
        Page<Vote> votePage = new Page<>(pageIndex,size);
        Page<Vote> page = new LambdaQueryChainWrapper<>(service.getBaseMapper()).eq(Vote::getUserId,userId).page(votePage);
        return Results.success(page);
    }
    @PostMapping("saveVote")
    public Results saveVote(@RequestBody Vote vote){
        vote.setCreateTime(LocalDateTime.now());
        boolean save = service.save(vote);
        return Results.success(save);
    }
    @GetMapping("getVoteById/{id}")
    public Results getVoteById(@PathVariable Integer id){
        VoteDto voteById = service.getVoteById(id);
        return Results.success(voteById);
    }
    @GetMapping("getVoteRanking")
    public Results getVoteRanking(){
        return Results.success(service.getVoteRanking());
    }

    @DeleteMapping("/deleteVote/{id}")
    public Results deleteVote(@PathVariable Integer id){
        return Results.success(service.deleteVote(id));
    }

    @PutMapping("updateVote")
    public Results updateVote(@RequestBody Vote vote){
        return Results.success(service.updateById(vote));
    }
}
